package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void viewnumIncr(String id) {
        StrategyStatisVO vo = this.getStrategyStatisVO(id);
        vo.setViewnum(vo.getViewnum() + 1);
        this.setStrategyStatisVO(vo);
    }

    @Override
    public StrategyStatisVO getStrategyStatisVO(String sid) {
        //拼接key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //查询数据库是否有该key的记录
        StrategyStatisVO vo = null;
        if(redisTemplate.hasKey(key)){
            String voStr = redisTemplate.opsForValue().get(key);
            vo = JSON.parseObject(voStr,StrategyStatisVO.class);
        }else{
            //没有从数据库查出阅读量,返回
            Strategy strategy = strategyService.find(sid);
            vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
        }
        return vo;
    }

    @Override
    public void setStrategyStatisVO(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public void replynumIncr(String strategyId) {
        //根据攻略id获取vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(strategyId);
        //vo对象回复数加1
        vo.setReplynum(vo.getReplynum() + 1);
        //vo对象设置到redis中
        this.setStrategyStatisVO(vo);
    }

    @Override
    public boolean strategyFavorExist(String sid, UserInfo userInfo) {
        //获取攻略统计vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        List<String> strategyList = userInfoRedisService.getUserStrategyFavorList(userInfo.getId());
        //判断列表是否为空或者sid不存在于攻略收藏列表中
        if(strategyList.size() == 0 || !strategyList.contains(sid)){
            //是为收藏操作,vo对象的收藏数+1,sid添加到列表中
            vo.setFavornum(vo.getFavornum() + 1);
            strategyList.add(sid);
        }else{
            //否则为取消收藏操作,vo对象的收藏数-1,sid从列表中移除
            vo.setFavornum(vo.getFavornum() - 1);
            strategyList.remove(sid);
        }
        //更新redis中数据
        this.setStrategyStatisVO(vo);
        userInfoRedisService.setUserStrategyFavorList(userInfo,strategyList);
        return strategyList.contains(sid);
    }

    @Override
    public boolean strategyThumbupExist(String sid, String uid) {
        //获取攻略统计vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        //拼接出key
        String key = RedisKeys.USER_STRATEGY_THUMBUP.join(uid,sid);
        //查询数据库中是否存在该key
        if(!redisTemplate.hasKey(key)){
            //如果不存在,表示没点赞过,将key加入redis
            redisTemplate.opsForValue().set(key,"1",DateUtil.getDateBetween(new Date(),DateUtil.getEndDate(new Date())),TimeUnit.SECONDS);
            //vo对象中的Thumbupnum加1
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            //进入该方法返回true,表示点赞成功
            //存在不进行任何操作
            this.setStrategyStatisVO(vo);
            return true;
        }
        return false;
    }

    @Override
    public boolean isStrategyStatisVOExist(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        return redisTemplate.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyStatisVos() {
        //查询redis中所有攻略vo对象
        Set<String> voKeys = redisTemplate.keys(RedisKeys.STRATEGY_STATIS_VO.join("*"));
        List<StrategyStatisVO> list = new ArrayList<>();
        if(voKeys != null && voKeys.size() > 0){
            for (String voKey : voKeys) {
                String strVo = redisTemplate.opsForValue().get(voKey);
                StrategyStatisVO vo = JSON.parseObject(strVo, StrategyStatisVO.class);
                list.add(vo);
            }
        }
        return list;
    }


}
